﻿

let GoodsLable = {
    GTid: "",
    name: ""
};

function BindTable(obj)
{
    layui.use('table', function ()
    {
        var table = layui.table;

        table.render({
            elem: '#test'
            , data: obj
            /* , cellMinWidth: 80*/ //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[

                { field: 'name', title: '标签名' },
                { field: 'goodsTypeName', title: '商品类型' },
                { field: 'goodsCount', title: '商品数量' },
                { title: '操作', toolbar: "#barDemo" }
            ]]
        });


        table.on('tool(ts)', function (obj)
        {
            var data = obj.data;
            var layEvent = obj.event;
            switch (layEvent) {
                case "detail":
                    sessionStorage.setItem("id", data.id);
                    location.href = "/Goods/GoodHtml";
                    break;
                case "update":

                    layer.open
                    ({
                        title: '修改标签',
                        area: ['800px', '400px'],
                        content:
                            ` <div class="left">
                                <div class="Input-Control">
                                    <div class="input-text"><p>标签名</p></div>
                                    <input type="text" required placeholder="请输入标签名称" autocomplete="off" id="GoodLabelName" name="name" value=${data.name} />
                                </div>
                                 <div class="Input-Control">
                                     <div class="input-text"><p>标签类型</p></div>
                                     <div id="GoodsTypeSelect" style="width:80%;" class="Select-Control"></div>
                                 </div>
                            </div>`,
                        yes: function (index)
                        {
                            $.post("/Goods/UpdateGoodLabel", {
                                id: data.id,
                                name: $("#GoodLabelName").val(),
                                Gtid: GoodsLable.GTid
                                
                            }).then(res =>
                            {
                                if (res.code == 0)
                                {
                                    Peng.Show(res.message, { type: "Success" });
                                    layer.close(index);
                                    GetGoodslabelByGTid();
                                }
                                else
                                {
                                    Peng.Show(res.message, { type: "Danger" });

                                }
                            })
                        }
                    })

                    $.get("/Goods/GetGoodsType").then((res) => {
                        Peng_Select.CreateElement("GoodsTypeSelect", res.data, "name", (res) => {
                            GoodsLable.GTid = res.id;
                        });
                    });


                    break;
            }
        });
    });







}


var id = sessionStorage.getItem("id")

function GetGoodslabelByGTid()
{
    $.get("/Goods/GetGoodslabelByGTid", { id: id, keys: "" }).then((res) =>
    {
        BindTable(res.data)
    })
}
GetGoodslabelByGTid();


var query = () =>
{
    var key = $("#Tnumber").val();
    $.get("/Goods/GetGoodslabelByGTid", { id: id, keys: key }).then((res) => {
        BindTable(res.data)
    })

}



